<?php
class EmailController extends AppController {
	
	var $name = 'Email';
	var $components = array('Auth', 'Email', 'Session');
	var $uses = null;
	var $layout = '';
	
	function beforeFilter(){
		$this->Auth->allow('send');
	}
	
	public function send() {
		
		// captcha check
		if($this->params['form']['captcha'] != $this->Session->read('captcha')){
			$this->Session->setFlash($this->__('Repeat the code.'));
			$this->redirect($this->referer());
			return ;
		}
		
		// name email validation
		if( !$this->params['form']['name'] || !$this->params['form']['email']){
			$this->Session->setFlash($this->__('Name and Email are mandatory fields.'));
			$this->redirect($this->referer());
			return ;
		}
		
		$this->Configuration =& ClassRegistry::init('Configuration');

		$config_to 				= $this->Configuration->find(array('name'=>'email_to'));
		$config_from 			= $this->Configuration->find(array('name'=>'email_from'));
		$config_subject 		= $this->Configuration->find(array('name'=>'email_subject'));
		$config_max_file_size 	= $this->Configuration->find(array('name'=>'email_max_attachment_size'));
		
		$config_to 				= $config_to 			? $config_to['Configuration']['value'] 				: 'bulgariansp@gmail.com';
		$config_from 			= $config_from 			? $config_from['Configuration']['value'] 			: 'site@bulgariansp.com';
		$config_subject 		= $config_subject 		? $config_subject['Configuration']['value'] 		: 'Default Subject';
		$config_max_file_size 	= $config_max_file_size ? $config_max_file_size['Configuration']['value'] 	: '2097152';
				
		// file size validation
		if( ($this->params['form']['file']['name'] && $this->params['form']['file']['size'] == 0) || $this->params['form']['file']['size'] > intval($config_max_file_size)) {
			$this->Session->setFlash($this->__('File is larger 2MB'));
			$this->redirect($this->referer());
			return ;
		}
		
		/* @var $emailComponent EmailComponent  */
		$emailComponent = $this->Email;
		
		$emailComponent->to 		= $config_to;
		$emailComponent->from 		= $config_from;
		$emailComponent->subject 	= $config_subject;
		
		if($this->params['form']['file']['tmp_name'])
			$emailComponent->attachments = array($this->params['form']['file']['name'] => $this->params['form']['file']['tmp_name']);
		
		$name = $this->params['form']['name'];
		$email = $this->params['form']['email'];
		$telephone = $this->params['form']['telephone'];
		$firm_name = $this->params['form']['firm_name'];
		$inn_kkp = $this->params['form']['inn_kkp'];
		$message = $this->params['form']['message'];
		
		$content = "
			Name: $name 
			Email: $email 
			Telephone: $telephone 
			Firm name: $firm_name 
			INN KKP: $inn_kkp 
			Message: $message 
		";
		
		$emailComponent->send($content);
		
		if (file_exists($this->params['form']['file']['tmp_name']))
			unlink($this->params['form']['file']['tmp_name']);
		
		$this->redirect($this->referer());
		
	}
	
	public function delete($id = NULL) {
		if ($id) {
			if ($this->Asset->delete ( $id )) {
				$this->Session->setFlash ( 'File deleted successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed! Please try again or contact to administrator.' );
				$this->redirect ( array ('action' => 'index' ) );
			}
		
		} else {
			$this->Session->setFlash ( 'Failed! No such file.' );
			$this->redirect ( array ('action' => 'index' ) );
		
		}
	}
	
	public function add() {
		if (! empty ( $this->data )) {
			$this->Asset->create ();
			
			$saveable = array(
				"name" => $this->data['Asset']['name'],
			    "group" => ($this->data ['Asset']['group'] == -2) ? $this->data['Asset']['group_other'] : $this->data ['Asset']['group'],
			);
			
			
			if($this->data['Asset']['image'] && $this->data['Asset']['image']['size']!=0){
				$fileOk	= $this->SimpleUpload->uploadFiles($this->Asset->save_path, array( $this->data['Asset']['image']));
				if(array_key_exists('errors', $fileOk))
				{
					$sErrorMessages = implode(',', $fileOk['errors']);
					$this->Session->setFlash( $sErrorMessages );
					return;
				}else if(array_key_exists('urls',$fileOk))
				{
					$saveable['url'] = $fileOk['urls'][0]; 
					$saveable['filename'] = $this->data['Asset']['image']['name']; 
				}
			}
			if ($this->Asset->save ( $saveable )) {
				$this->Session->setFlash ( 'File added successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed to add file!' );
			}
		}
	}
	
}

?>